
/////////////////////////////////////////////////////////////
//// Tables and Figures for 'Strangers in Hostile Lands' ////
//// Schaub, Max; Baldassarri, Delia; Gereke, Johanna    ////
//// Forthcoming in Comparative Political Studies        ////
/////////////////////////////////////////////////////////////



*****Installing packages********
ssc install revrs, replace
ssc install fre, replace
ssc install estout, replace
ssc install coefplot, replace
ssc install blindschemes, replace
ssc install SPWMATRIX, replace
ssc install spmlreg, replace
ssc install splagvar, replace 
ssc install devnplot, replace


* Set working directory
cap cd ""


//////////////////////////////////////////////
********Tables and Figures main paper*********
//////////////////////////////////////////////

* Figure 1, 2, 3a are not replicated here

**** Figure 3b ****
*** Nr of refugees in host municipalities 2015***
use "dta_files/ind.dta", clear
preserve
foreach v of var * { 
	local l`v' : variable label `v' 
}
collapse (mean) recip15_per_1000, by(id_gemeinde)
foreach v of var * { 
	label var `v' "`l`v''" 
} 
hist recip15_per_1000 if recip15_per_1000>4 & recip15_per_1000<75, bin(30)  xscale(range(0 (5) 70)) freq xmtick(5 (5) 75) xlab(0 (10) 75) xline(4) aspectratio(1.2) ytitle("Nr of municipalities")
graph export "output/Fig3b.pdf", replace
restore 


****** Table 1 *****
*** Municipality level 
use "dta_files/ind.dta", clear

* Covariates
preserve
foreach v of var * { 
	local l`v' : variable label `v' 
}
collapse (mean) km2 pop14 popdens14 unempl_100 frauen_a alter_d ab65_a share_m_15_25 auslaender_a kkr_ewa05_1000 part_rate shareCDU2013 shareSPD2013 shareLINKE2013 shareAFD2013  npartysimple ruling_party_affil leerstandsquote share_CDU2017 share_SPD2017 share_LINKE2017 share_AFD2017 turnout17 n_recipients2014 recip14_per_1000 n_recipients2015 recip15_per_1000 n_recipients2016 percpop_recip16 demonstration arson vandalism assault sn_l sfemales15 sreception15 scommunal15 sdecentral15 treat, by(id_gemeinde)
foreach v of var * { 
	label var `v' "`l`v''" 
} 
bys treat: egen obs = count(id_gemeinde)
label variable obs "N"

* Treatment 
eststo overalltreat: estpost tabstat n_recipients2014 recip14_per_1000 n_recipients2015 recip15_per_1000 n_recipients2016 percpop_recip16, columns(statistics) stats(mean sd) missing  
eststo bygrouptreat: estpost tabstat n_recipients2014 recip14_per_1000 n_recipients2015 recip15_per_1000 n_recipients2016 percpop_recip16 obs, by(treat) columns(statistics) stats(mean sd) missing nototal 
esttab overalltreat bygrouptreat,  main(mean %9.2f) aux(sd %9.2f) unstack  nonote  noobs compress label nogaps  title("Treatment indicators") nonumber varwidth(30) replace wide booktabs

* Pretreatment covariates, additional covariates and election results 2013 (Table 1)
eststo overallm: estpost tabstat km2 pop14 popdens14 unempl_100 frauen_a alter_d ab65_a share_m_15_25 auslaender_a kkr_ewa05_1000 part_rate shareCDU2013 shareSPD2013 shareLINKE2013 shareAFD2013 npartysimple ruling_party_affil leerstandsquote, columns(statistics) stats(mean sd) missing  
eststo bygroupm: estpost tabstat km2 pop14 popdens14 unempl_100 frauen_a alter_d ab65_a share_m_15_25 auslaender_a kkr_ewa05_1000 part_rate shareCDU2013 shareSPD2013 shareLINKE2013 shareAFD2013 npartysimple ruling_party_affil leerstandsquote obs, by(treat) columns(statistics) stats(mean sd) missing nototal 
esttab overallm bygroupm,  main(mean %9.2f) aux(sd %9.2f) unstack  nonote  noobs compress label nogaps  title("Municipality-level covariates") nonumber varwidth(30) replace  wide booktabs

restore

*** Individual level
* Covariates
eststo overall: estpost tabstat female age partnership parent hhmembers edu, columns(statistics) stats(mean sd) missing  
eststo bygroup: estpost tabstat female age partnership parent hhmembers edu obs, by(treat) columns(statistics) stats(mean sd) missing  nototal
esttab overall bygroup,  main(mean %9.2f) aux(sd %9.2f) unstack  nonote  noobs compress label nogaps  title("Individual-level covariates") nonumber varwidth(30) replace  wide booktabs

* Manipulation checks and attrition 
eststo overallout: estpost tabstat foreignersmunicip refugees , columns(statistics) stats(mean sd) missing  
eststo bygroupout: estpost tabstat foreignersmunicip refugees , by(treat) columns(statistics) stats(mean sd) missing nototal
esttab overallout bygroupout,  main(mean %9.2f) aux(sd %9.2f) unstack  nonote  noobs compress label nogaps  title("Manipulation checks and attrition") nonumber varwidth(45) replace  wide booktabs


**** Figure 4 ****
use "dta_files/ind.dta", clear
preserve

foreach v of var * { 
	local l`v' : variable label `v' 
}
collapse (mean) recip15_per_1000 female age partnership parent hhmembers edu  km2 pop14 popdens14 unempl_100 frauen_a alter_d ab65_a share_m_15_25 auslaender_a kkr_ewa05_1000 part_rate shareCDU2013 shareSPD2013 shareLINKE2013 shareAFD2013  npartysimple ruling_party_affil leerstandsquote share_CDU2017 share_SPD2017 share_LINKE2017 share_AFD2017 n_recipients2014 recip14_per_1000 n_recipients2015 sn_l sfemales15 sreception15 scommunal15 sdecentral15 brand demo koerperver unterkunft any_attack treat, by(gemeinde_no)
foreach v of var * { 
	label var `v' "`l`v''" 
}

** Bar graph(a): Election results 2013 and 2017***
gen placeholder1=20
gen placeholder2=20
gen placeholder3=20

graph bar (mean) shareCDU2013 share_CDU2017 placeholder1 shareSPD2013 share_SPD2017 placeholder2 ///
shareLINKE2013 share_LINKE2017 placeholder3 shareAFD2013 share_AFD2017, ///
bar(1, color(black*1.5) ) bar(2, color(black*1.5) ) ///
bar(4, color(red)) bar(5, color(red)) ///
bar(7, color(pink*1.3)) bar(8, color(pink*1.3)) ///
bar(10, color(midblue*1.3))  bar(11, color(midblue*1.3)) bargap(22) blabel(bar, position(inside)) legend(position(2))

graph display , ysize(1) xsize(1.9) scale(1.25) scheme(plotplain) 
graph export "output/Fig4a.pdf", replace

** Treatment effect 2017 (b)
reg share_CDU2017 treat
eststo CDU
reg share_SPD2017 treat
eststo SPD
reg share_LINKE2017 treat
eststo LINKE
reg share_AFD2017 treat
eststo AfD

coefplot CDU SPD LINKE AfD, drop(_cons) ytitle("")
graph export "output/Fig4b.pdf", replace

** Treatment effect state elections 2016 (c)
merge 1:1 gemeinde_no using "dta_files/municip16.dta"
drop if _merge==2
eststo clear
eststo AfD: reg shareAFD2016 treat
eststo CDU: reg shareCDU2016 treat
eststo SPD: reg shareSPD2016 treat 
eststo LINKE: reg shareLINKE2016 treat

coefplot CDU SPD LINKE AfD, drop(_cons) ytitle("") xline(0) aspectratio(1.1) xscale(range(-6 (2) 6)) xlabel(-6 (2) 6, format(%03.1f))
graph export "output/Fig4c.pdf", replace

restore


****** Table 2: Outcomes *******
* Municpality level outcomes 
* Election results 2017
use "dta_files/ind.dta", clear

preserve

foreach v of var * { 
	local l`v' : variable label `v' 
}
collapse (mean) km2 pop14 popdens14 unempl_100 frauen_a alter_d ab65_a share_m_15_25 auslaender_a kkr_ewa05_1000 part_rate shareCDU2013 shareSPD2013 shareLINKE2013 shareAFD2013  npartysimple ruling_party_affil leerstandsquote share_CDU2017 share_SPD2017 share_LINKE2017 share_AFD2017 turnout17 n_recipients2014 recip14_per_1000 n_recipients2015 recip15_per_1000 n_recipients2016 percpop_recip16 demonstration arson vandalism assault sn_l sfemales15 sreception15 scommunal15 sdecentral15 treat, by(id_gemeinde)
foreach v of var * { 
	label var `v' "`l`v''" 
} 
bys treat: egen obs = count(id_gemeinde)
label variable obs "N"

eststo overallmout: estpost tabstat share_CDU2017 share_SPD2017 share_LINKE2017 share_AFD2017 turnout17, columns(statistics) stats(mean sd) missing  
eststo bygroupmout: estpost tabstat share_CDU2017 share_SPD2017 share_LINKE2017 share_AFD2017 turnout17 obs, by(treat) columns(statistics) stats(mean sd) missing nototal 
esttab overallmout bygroupmout,  main(mean %9.2f) aux(sd %9.2f) unstack  nonote  noobs compress label nogaps  title("Municipality-level outcomes") nonumber varwidth(30) replace wide booktabs

restore 

* Individual level outcomes (Can delete "ingroup bias division game")
eststo overallout: estpost tabstat dg_inbias_nstd tg_inbias_nstd split_inbias_nstd petition ethnocent_assert ethnocent_proud xenophob_exploit xenophob_welfare foreigner_work foreigner_pension rwvote afdthermometer pop_people pop_compro pop_satis refrestr_war refrestr_persec refrestr_econ refrestr_fam refcompete refdeport refprotect, columns(statistics) stats(mean sd) missing  
eststo bygroupout: estpost tabstat dg_inbias_nstd tg_inbias_nstd split_inbias_nstd petition ethnocent_assert ethnocent_proud xenophob_exploit xenophob_welfare foreigner_work foreigner_pension rwvote afdthermometer pop_people pop_compro pop_satis refrestr_war refrestr_persec refrestr_econ refrestr_fam refcompete refdeport refprotect, by(treat) columns(statistics) stats(mean sd) missing nototal
esttab overallout bygroupout,  main(mean %9.2f) aux(sd %9.2f) unstack  nonote  noobs compress label nogaps  title("Outcome measures") nonumber varwidth(45) replace  wide booktabs



//////////Regression results////////
use "dta_files/ind.dta", clear

local ics female age i.partnership parent hhmembers edu 
local mcs km2 pop14 popdens14 unempl_100 frauen_a alter_d ab65_a share_m_15_25 auslaender_a kkr_ewa05_1000 part_rate shareCDU2013 shareSPD2013 shareLINKE2013 shareAFD2013 i.sn_l 

** Individual level 
* binary treatment 
eststo clear
reg biasscale treat `ics' `mcs', vce(cluster id_gemeinde)
eststo biasscale
reg rwscale treat `ics' `mcs', vce(cluster id_gemeinde)
eststo rwscale
reg popscale treat `ics' `mcs', vce(cluster id_gemeinde)
eststo popscale
reg refscale treat `ics' `mcs', vce(cluster id_gemeinde)
eststo refscale

ksmirnov biasscale, by(treat)
ksmirnov rwscale, by(treat)
ksmirnov popscale, by(treat)
ksmirnov refscale, by(treat)

* percentages
reg biasscale percpop_recip15 `ics' `mcs', vce(cluster id_gemeinde)
eststo biasscalep
reg rwscale percpop_recip15 `ics' `mcs', vce(cluster id_gemeinde)
eststo rwscalep
reg popscale percpop_recip15 `ics' `mcs', vce(cluster id_gemeinde)
eststo popscalep
reg refscale percpop_recip15 `ics' `mcs', vce(cluster id_gemeinde)
eststo refscalep

** Municipality level
preserve

merge m:1 gemeinde_no using "dta_files/municip16.dta", keepusing(shareCDU2016 shareSPD2016 shareLINKE2016 shareAFD2016 shareGREENS2016 shareFDP2016)
drop if _merge==2
drop _merge

foreach v of var * { 
	local l`v' : variable label `v' 
}
collapse (mean) female age partnership parent hhmembers edu  km2 pop14 popdens14 unempl_100 frauen_a alter_d ab65_a share_m_15_25 auslaender_a kkr_ewa05_1000 part_rate shareCDU2013 shareSPD2013 shareLINKE2013 shareAFD2013  npartysimple ruling_party_affil leerstandsquote share_CDU2017 share_SPD2017 share_LINKE2017 share_AFD2017 share_afd2017 n_recipients2014 recip14_per_1000 n_recipients2015 percpop_recip15 recip15_per_1000 sn_l sfemales15 sreception15 scommunal15 sdecentral15 brand demo koerperver unterkunft any_attack shareCDU2016 shareSPD2016 shareLINKE2016 shareAFD2016 shareGREENS2016 shareFDP2016 treat, by(id_gemeinde)
foreach v of var * { 
	label var `v' "`l`v''" 
}

local mcs km2 pop14 popdens14 unempl_100 frauen_a alter_d ab65_a share_m_15_25 auslaender_a kkr_ewa05_1000 part_rate shareCDU2013 shareSPD2013 shareLINKE2013 shareAFD2013 i.sn_l 
gen treat_5=treat


* binary treatment
reg share_AFD2017 treat_5 `mcs'
gen share_afd201710 = share_afd2017*10
reg share_afd201710 treat `mcs'
eststo share_AFD201710
reg share_AFD2017 treat `mcs'
eststo share_AFD2017
ksmirnov share_AFD2017, by(treat)

* Percentages
reg share_AFD2017 percpop_recip15 `mcs'
eststo share_AFD2017p
reg share_AFD2017 percpop_recip15  `mcs' if percpop_recip15<20
eststo share_AFD2017p_no_outlier


*** Figure 5****
coefplot share_AFD201710, bylabel(AfD 2017) xline(0) ///
		|| rwscale, bylabel(Right-wing support) /// 
		|| refscale, bylabel(Anti-refugee sentiments) /// 
		|| popscale, bylabel(Populism) /// 
		|| biasscale, bylabel(Behavioral bias) ///  
keep(treat) byopts(cols(3) hole(4)) xscale(range(-.1 (.05)  .1)) xlabel(-.1 (.05) .1, format(%4.2f))  name(Fig5, replace) ///
ylab("") subtitle( ,bcolor(white) bmargin(bottom) lcolor(gs8))  aspectratio(0.5) 
graph display Fig5, ysize(1) xsize(1.9) scale(1.25) scheme(plotplain) 
graph export "output/Fig5.pdf", replace

restore

*******Results theoretical mechanisms******
***Table 3***
** Pairwise correlations
use "dta_files/ind.dta", clear
estpost correlate rwscale refscale popscale biasscale deprivationscale fearscale alienationscale discordscale empathy contactscale, matrix listwise 
esttab ., unstack not noobs compress b(a2) nostar booktabs label

***Figure 6***
** Regression of mechanisms on treatment (Figure 6, Table 9A)
local ics female age i.partnership parent hhmembers edu 
local mcs km2 pop14 popdens14 unempl_100 frauen_a alter_d ab65_a share_m_15_25 auslaender_a kkr_ewa05_1000 part_rate shareCDU2013 shareSPD2013 shareLINKE2013 shareAFD2013 i.sn_l

reg deprivationscale treat  `ics' `mcs', vce(cluster id_gemeinde) 
eststo deprivationscale
reg fearscale treat  `ics' `mcs', vce(cluster id_gemeinde) 
eststo fearscale
reg alienationscale treat  `ics' `mcs', vce(cluster id_gemeinde) 
eststo alienationscale
reg discordscale treat  `ics' `mcs', vce(cluster id_gemeinde) 
eststo discordscale
reg empathy treat  `ics' `mcs', vce(cluster id_gemeinde) 
eststo empathy
reg contactscale treat `ics' `mcs', vce(cluster id_gemeinde) 
eststo contactscale


* Figure 6
coefplot deprivationscale, bylabel(Deprivation/competition) xline(0)  ///
		|| fearscale, bylabel(Insecurity) /// 
		|| alienationscale, bylabel(Alienation) /// 
		|| discordscale, bylabel(Community/elite discord) /// 
		|| empathy, bylabel(Empathy) ///  
        || contactscale, bylabel(Contact) ///  
keep(treat) byopts(cols(3)) xscale(range(-.15 (.05)  .15)) xlabel(-.15 (.05)  .15) name(Fig6, replace) aspectratio(0.5) ///
ylab("", notick) yscale(lstyle(none))  subtitle( ,bcolor(white) bmargin(bottom) lcolor(gs8)) 
graph display Fig6, ysize(1) xsize(1.9) scale(1.25) scheme(plotplain) 
graph export "output/Fig6.pdf", replace


/////////////////////////////////////////////
**********Treatment heterogeneity************
/////////////////////////////////////////////

***Figure 7****
* See R_script/Fig7_causal_trees.R


****Figure 8*****
use "dta_files/ind.dta", clear

local ics female age i.partnership parent hhmembers edu 
local mcs km2 pop14 popdens14 unempl_100 frauen_a alter_d ab65_a share_m_15_25 auslaender_a kkr_ewa05_1000 part_rate shareCDU2013 shareSPD2013 shareLINKE2013 shareAFD2013 i.sn_l 

cap drop young_man
gen young_man=0
replace young_man=1 if agecat==0 & female==0

reg refscale treat##lrcat `ics' `mcs', vce(cluster id_gemeinde) 
margins lrcat##treat, post coefl
nlcom (_b[1bn.lrcat#1bn.treat] - _b[1bn.lrcat#0.treat]) (_b[2.lrcat#1.treat] - _b[2.lrcat#0.treat]) (_b[3.lrcat#1.treat] - _b[3.lrcat#0.treat]), post
eststo refscalelrcat

reg refscale treat##authoritarianism `ics' `mcs', vce(cluster id_gemeinde)  
margins authoritarianism##treat, post coefl
nlcom (_b[1bn.authoritarianism#1bn.treat] - _b[1bn.authoritarianism#0.treat]) (_b[2.authoritarianism#1.treat] - _b[2.authoritarianism#0.treat]) (_b[3.authoritarianism#1.treat] - _b[3.authoritarianism#0.treat]), post
eststo refscaleauthoritarianism

reg refscale treat##agecats `ics' `mcs', vce(cluster id_gemeinde) 
margins agecats##treat, post coefl
nlcom (_b[1bn.agecats#1bn.treat] - _b[1bn.agecats#0.treat]) (_b[2.agecats#1.treat] - _b[2.agecats#0.treat]) (_b[3.agecats#1.treat] - _b[3.agecats#0.treat]), post
eststo refscaleagecats

reg refscale treat##female `ics' `mcs', vce(cluster id_gemeinde) 
margins female##treat, post coefl
nlcom (_b[1.female#1.treat] - _b[1.female#0.treat]) (_b[0.female#1.treat] - _b[0.female#0.treat]), post
eststo refscalegender

reg refscale treat##young_man `ics' `mcs', vce(cluster id_gemeinde) 
margins young_man##treat, post coefl
nlcom (_b[1.young_man#1bn.treat] - _b[1.young_man#0.treat]), post
eststo refscaleym


coefplot (refscalelrcat, rename(_nl_1 = "Left" _nl_2 = "Center" _nl_3 = "Right") ///
 xline(0) legend(off)) ///
  (refscaleym, noci) ///
(refscaleauthoritarianism, rename(_nl_1 = "Libertarian" _nl_2 = "Moderate" _nl_3 = "Authoritarian")), ///
title("Anti-refugee sentiments", size(medium)) aspectratio(1) ciopts(recast(rcap)) nooffset name(hetref, replace)


reg rwscale treat##lrcat `ics' `mcs', vce(cluster id_gemeinde) 
margins lrcat##treat, post coefl
nlcom (_b[1bn.lrcat#1bn.treat] - _b[1bn.lrcat#0.treat]) (_b[2.lrcat#1.treat] - _b[2.lrcat#0.treat]) (_b[3.lrcat#1.treat] - _b[3.lrcat#0.treat]), post
eststo rwscalelrcat

reg rwscale treat##authoritarianism `ics' `mcs', vce(cluster id_gemeinde)  
margins authoritarianism##treat, post coefl
nlcom (_b[1bn.authoritarianism#1bn.treat] - _b[1bn.authoritarianism#0.treat]) (_b[2.authoritarianism#1.treat] - _b[2.authoritarianism#0.treat]) (_b[3.authoritarianism#1.treat] - _b[3.authoritarianism#0.treat]), post
eststo rwscaleauthoritarianism

reg rwscale treat##agecats `ics' `mcs', vce(cluster id_gemeinde) 
margins agecats##treat, post coefl
nlcom (_b[1bn.agecats#1bn.treat] - _b[1bn.agecats#0.treat]) (_b[2.agecats#1.treat] - _b[2.agecats#0.treat]) (_b[3.agecats#1.treat] - _b[3.agecats#0.treat]), post
eststo rwscaleagecats

reg rwscale treat##female `ics' `mcs', vce(cluster id_gemeinde) 
margins female##treat, post coefl
nlcom (_b[1.female#1.treat] - _b[1.female#0.treat]) (_b[0.female#1.treat] - _b[0.female#0.treat]), post
eststo rwscalegender

reg rwscale treat##young_man `ics' `mcs', vce(cluster id_gemeinde) 
margins young_man##treat, post coefl
nlcom (_b[1.young_man#1bn.treat] - _b[1.young_man#0.treat]), post
eststo rwscaleym

coefplot (rwscalelrcat, rename(_nl_1 = "Left" _nl_2 = "Center" _nl_3 = "Right") ///
 xline(0) legend(off)) ///
  (refscaleym, noci) ///
(rwscaleauthoritarianism, rename(_nl_1 = "Libertarian" _nl_2 = "Moderate" _nl_3 = "Authoritarian")), ///
title("Right-wing support", size(medium)) aspectratio(1) ciopts(recast(rcap)) nooffset name(hetrw, replace)

reg biasscale treat##lrcat `ics' `mcs', vce(cluster id_gemeinde) 
margins lrcat##treat, post coefl
nlcom (_b[1bn.lrcat#1bn.treat] - _b[1bn.lrcat#0.treat]) (_b[2.lrcat#1.treat] - _b[2.lrcat#0.treat]) (_b[3.lrcat#1.treat] - _b[3.lrcat#0.treat]), post
eststo biasscalelrcat

reg biasscale treat##authoritarianism `ics' `mcs', vce(cluster id_gemeinde)  
margins authoritarianism##treat, post coefl
nlcom (_b[1bn.authoritarianism#1bn.treat] - _b[1bn.authoritarianism#0.treat]) (_b[2.authoritarianism#1.treat] - _b[2.authoritarianism#0.treat]) (_b[3.authoritarianism#1.treat] - _b[3.authoritarianism#0.treat]), post
eststo biasscaleauthoritarianism

reg biasscale treat##agecats `ics' `mcs', vce(cluster id_gemeinde) 
margins agecats##treat, post coefl
nlcom (_b[1bn.agecats#1bn.treat] - _b[1bn.agecats#0.treat]) (_b[2.agecats#1.treat] - _b[2.agecats#0.treat]) (_b[3.agecats#1.treat] - _b[3.agecats#0.treat]), post
eststo biasscaleagecats

reg biasscale treat##female `ics' `mcs', vce(cluster id_gemeinde) 
margins female##treat, post coefl
nlcom (_b[1.female#1.treat] - _b[1.female#0.treat]) (_b[0.female#1.treat] - _b[0.female#0.treat]), post
eststo biasscalegender

reg biasscale treat##young_man `ics' `mcs', vce(cluster id_gemeinde) 
margins young_man##treat, post coefl
nlcom (_b[1.young_man#1bn.treat] - _b[1.young_man#0.treat]), post
eststo biasscaleym

coefplot (biasscalelrcat, rename(_nl_1 = "Left" _nl_2 = "Center" _nl_3 = "Right") ///
 xline(0) legend(off)) ///
  (refscaleym, noci) ///
(biasscaleauthoritarianism, rename(_nl_1 = "Libertarian" _nl_2 = "Moderate" _nl_3 = "Authoritarian")), ///
title("Behavioral bias", size(medium)) aspectratio(1) ciopts(recast(rcap)) nooffset  name(hetbias, replace)


reg popscale treat##lrcat `ics' `mcs', vce(cluster id_gemeinde) 
margins lrcat##treat, post coefl
nlcom (_b[1bn.lrcat#1bn.treat] - _b[1bn.lrcat#0.treat]) (_b[2.lrcat#1.treat] - _b[2.lrcat#0.treat]) (_b[3.lrcat#1.treat] - _b[3.lrcat#0.treat]), post
eststo popscalelrcat

reg popscale treat##authoritarianism `ics' `mcs', vce(cluster id_gemeinde)  
margins authoritarianism##treat, post coefl
nlcom (_b[1bn.authoritarianism#1bn.treat] - _b[1bn.authoritarianism#0.treat]) (_b[2.authoritarianism#1.treat] - _b[2.authoritarianism#0.treat]) (_b[3.authoritarianism#1.treat] - _b[3.authoritarianism#0.treat]), post
eststo popscaleauthoritarianism

reg popscale treat##agecats `ics' `mcs', vce(cluster id_gemeinde) 
margins agecats##treat, post coefl
nlcom (_b[1bn.agecats#1bn.treat] - _b[1bn.agecats#0.treat]) (_b[2.agecats#1.treat] - _b[2.agecats#0.treat]) (_b[3.agecats#1.treat] - _b[3.agecats#0.treat]), post
eststo popscaleagecats

reg popscale treat##female `ics' `mcs', vce(cluster id_gemeinde) 
margins female##treat, post coefl
nlcom (_b[1.female#1.treat] - _b[1.female#0.treat]) (_b[0.female#1.treat] - _b[0.female#0.treat]), post
eststo popscalegender

reg popscale treat##young_man `ics' `mcs', vce(cluster id_gemeinde) 
margins young_man##treat, post coefl
nlcom (_b[1.young_man#1bn.treat] - _b[1.young_man#0.treat]), post
eststo popscaleym

coefplot (popscalelrcat, rename(_nl_1 = "Left" _nl_2 = "Center" _nl_3 = "Right") ///
 xline(0) legend(off)) ///
 (refscaleym, noci) ///
(popscaleauthoritarianism, rename(_nl_1 = "Libertarian" _nl_2 = "Moderate" _nl_3 = "Authoritarian")), ///
title("Populism", size(medium)) aspectratio(1) ciopts(recast(rcap)) nooffset name(hetpop, replace)


graph combine hetrw hetref hetpop hetbias, col(2) name(hetbiasrw, replace)
graph combine hetpop hetbias, col(2) name(hetpopref, replace)


*** Pooled outcomes***
* Slopes varying by individual outcome
reshape wide
cap drop outcome*
* Behavioral bias
gen outcome1 = dg_inbias
gen outcome2 = tg_inbias
* Right-wing support 
egen outcome3 = std(ethnocent_assert) 
egen outcome4 = std(ethnocent_proud)
egen outcome5 = std(xenophob_exploit)
egen outcome6 = std(xenophob_welfare)
egen outcome7 = std(foreigner_work)
egen outcome8 = std(foreigner_pension)
egen outcome9 = std(rwvote)
egen outcome10 = std(afdthermometer)
* Populism
egen outcome11 = std(pop_people)
egen outcome12 = std(pop_compro)
egen outcome13 = std(pop_satis)
* Reject refugees
egen outcome14 = std(refrestr_war)
egen outcome15 = std(refrestr_persec)
egen outcome16 = std(refrestr_econ)
egen outcome17 = std(refrestr_fam)
egen outcome18 = std(refcompete)
egen outcome19 = std(refdeport)
egen outcome20 = std(refprotect)

cap drop i
cap drop j
gen i = _n
reshape long outcome, i(i) j(j)

local ics female age i.partnership parent hhmembers edu 
local mcs km2 pop14 popdens14 unempl_100 frauen_a alter_d ab65_a share_m_15_25 auslaender_a kkr_ewa05_1000 part_rate shareCDU2013 shareSPD2013 shareLINKE2013 shareAFD2013 i.sn_l 

* Treatment effect across all outcomes
mixed outcome i.treat `ics' `mcs' ||j:
margins treat, post coefl
nlcom (_b[1.treat] - _b[0bn.treat]), post
eststo outcomeall

* Mock coefficient
quietly reg refscale treat##female `ics' `mcs'
quietly margins female##treat, post coefl
quietly nlcom (_b[1.female#1bn.treat] - _b[1.female#0.treat]), post
eststo gender

* Heterogeneous treatment effects across all outcomes
mixed outcome treat##lrcat `ics' `mcs' ||j:
margins lrcat##treat, post coefl
nlcom (_b[1bn.lrcat#1bn.treat] - _b[1bn.lrcat#0.treat]) (_b[2.lrcat#1.treat] - _b[2.lrcat#0.treat]) (_b[3.lrcat#1.treat] - _b[3.lrcat#0.treat]), post
eststo outcomelrcat

mixed outcome treat##authoritarianism `ics' `mcs' ||j:
margins authoritarianism##treat, post coefl
nlcom (_b[1bn.authoritarianism#1bn.treat] - _b[1bn.authoritarianism#0.treat]) (_b[2.authoritarianism#1.treat] - _b[2.authoritarianism#0.treat]) (_b[3.authoritarianism#1.treat] - _b[3.authoritarianism#0.treat]), post
eststo outcomeauthoritarianism

* Plot
coefplot (outcomeall, rename(_nl_1 = "Overall") ///
xline(0) legend(off)) ///
  (gender, noci rename(_nl_1 = "empty1")) ///
(outcomelrcat, rename(_nl_1 = "Left" _nl_2 = "Center" _nl_3 = "Right")) ///
  (gender, noci rename(_nl_1 = "empty2")) ///
(outcomeauthoritarianism, rename(_nl_1 = "Libertarian" _nl_2 = "Moderate" _nl_3 = "Authoritarian")), ///
xscale(range(-.2 (.1)  .3)) xlabel(-.2 (.1)  .3) ///
title("Individual-level outcomes (pooled)", size(medium)) nooffset name(pooledoutcomes, replace)
graph export "output/hetref.pdf", replace

* Combined with individual outcome-plots (Figure 8)
coefplot (outcomelrcat, rename(_nl_1 = "Left" _nl_2 = "Center" _nl_3 = "Right") ///
	xline(0) legend(off)) ///
  (refscaleym, noci) ///
	(outcomeauthoritarianism, rename(_nl_1 = "Libertarian" _nl_2 = "Moderate" _nl_3 = "Authoritarian")), ///
	title("Pooled outcomes", size(medium)) aspectratio(1) ciopts(recast(rcap)) nooffset name(pooledoutcomes1, replace)

* Figure 8	
graph combine hetrw hetref pooledoutcomes1 hetpop hetbias, col(3) name(Fig8, replace) ycommon
* Graph has to be manually edited
graph export "output/Fig8.pdf", replace








////////////////////////////////////////
********Supporing information*********
///////////////////////////////////////


***Figure 9A*****
use "dta_files/asylum1990_2017.dta", clear
tsset year 
format %8.0fc applications
tsline applications, title("") legend(ring(0) position(10) bmargin(large)) ///
xscale(range(1990 2017)) xlabel(1990 (3) 2017) xmtick(1990 (1) 2017) ///
name(asylum, replace)
graph export "output/Fig9A.pdf", replace


***Table 4A*****
*Table 4A data is in excel file "Table_4a" 


*****Measurment of mechanism******
* Table 5A: Summary mechanisms
use "dta_files/ind.dta", clear
eststo overallmech: estpost tabstat prime_deprive headscarf_bg headscarf_division alien_enrich alien_stranger alien_islam alien_muslim depr_lowinc depr_unempl depr_finan depr_unfair depr_second depr_losing contactrefugees supportrefugees2 aquaintances disc_polit disc_comm disc_discuss disc_dem disc_help empathy_nstd fear_alone fear_theft fear_violence, columns(statistics) stats(mean sd) missing   
eststo bygroupmech: estpost tabstat prime_deprive headscarf_bg headscarf_division alien_enrich alien_stranger alien_islam alien_muslim depr_lowinc depr_unempl depr_finan depr_unfair depr_second depr_losing contactrefugees supportrefugees2 aquaintances disc_polit disc_comm disc_discuss disc_dem disc_help empathy_nstd fear_alone fear_theft fear_violence, by(treat) columns(statistics) stats(mean sd) missing nototal 
esttab overallmech bygroupmech,  main(mean %9.2f) aux(sd %9.2f) unstack  nonote  noobs compress label nogaps  title("Mechanisms") nonumber varwidth(45) replace  wide booktabs


*******Heterogenous treatment effects on mechanisms*******
////////Figure 10A///////////
// Regression of mechanisms on treatment, separately for above and below 40 years old (and excluding controls)
use "dta_files/ind.dta", clear
cap drop age40
gen age40 = 1 if age>=40 
replace age40 = 0 if age<40
label define age40 0 "Aged 18-39" 1 "Aged 40 and older", replace
label values age40 age40

local ics female age i.partnership parent hhmembers edu 
local mcs km2 pop14 popdens14 unempl_100 frauen_a alter_d ab65_a share_m_15_25 auslaender_a kkr_ewa05_1000 part_rate shareCDU2013 shareSPD2013 shareLINKE2013 shareAFD2013 i.sn_l

reg deprivationscale treat  if age40==1, vce(cluster id_gemeinde)
eststo deprivationscale40over
reg fearscale treat if age40==1, vce(cluster id_gemeinde)
eststo fearscale40over
reg alienationscale treat if age40==1, vce(cluster id_gemeinde)
eststo alienationscale40over
reg discordscale treat if age40==1, vce(cluster id_gemeinde)
eststo discordscale40over
reg empathy treat if age40==1, vce(cluster id_gemeinde)
eststo empathy40over
reg contactscale treat if age40==1, vce(cluster id_gemeinde)
eststo contactscale40over

reg deprivationscale treat  if age40==0, vce(cluster id_gemeinde)
eststo deprivationscale40under
reg fearscale treat if age40==0, vce(cluster id_gemeinde)
eststo fearscale40under
reg alienationscale treat if age40==0, vce(cluster id_gemeinde)
eststo alienationscale40under
reg discordscale treat if age40==0, vce(cluster id_gemeinde)
eststo discordscale40under
reg empathy treat if age40==0, vce(cluster id_gemeinde)
eststo empathy40under
reg contactscale treat if age40==0, vce(cluster id_gemeinde)
eststo contactscale40under


coefplot (deprivationscale40over, label("40 years and over")) (deprivationscale40under, label("18 to 39 years")), bylabel(Deprivation/competition) xline(0) ///
		|| fearscale40over fearscale40under, bylabel(Insecurity) /// 
		|| alienationscale40over alienationscale40under, bylabel(Alienation) /// 
		|| discordscale40over discordscale40under, bylabel(Community/elite discord) /// 
		|| empathy40over empathy40under, bylabel(Empathy) ///  
        || contactscale40over contactscale40under,  bylabel(Contact) ///  
keep(treat) byopts(cols(3)) ylab("") xscale(range(-.5 (.25)  .5)) xlabel(-.5 (.25)  .5) name(mechanismplot40, replace) ///
aspectratio(0.5) subtitle( ,bcolor(white) bmargin(bottom) lcolor(gs8))  legend(rows(1)) 

graph display mechanismplot40, ysize(1) xsize(2.2) scale(1.3) scheme(plotplain) 
graph export "output/mechanismplot40.pdf", replace


///////////////Figure 11A/////////////
// Regression of mechanisms on treatment, separately for ideological groups 
use "dta_files/ind.dta", clear

***(a) Political orientation****
local ics female age i.partnership parent hhmembers edu 
local mcs km2 pop14 popdens14 unempl_100 frauen_a alter_d ab65_a share_m_15_25 auslaender_a kkr_ewa05_1000 part_rate shareCDU2013 shareSPD2013 shareLINKE2013 shareAFD2013 i.sn_l


reg deprivationscale treat   `ics' `mcs' if lrcat==1, vce(cluster id_gemeinde)
eststo deprivationscaleLeft
reg fearscale treat  `ics' `mcs' if lrcat==1, vce(cluster id_gemeinde)
eststo fearscaleLeft
reg alienationscale treat  `ics' `mcs' if lrcat==1, vce(cluster id_gemeinde)
eststo alienationscaleLeft
reg discordscale treat  `ics' `mcs' if lrcat==1, vce(cluster id_gemeinde)
eststo discordscaleLeft
reg empathy treat  `ics' `mcs' if lrcat==1, vce(cluster id_gemeinde)
eststo empathyLeft
reg contactscale treat `ics' `mcs' if lrcat==1, vce(cluster id_gemeinde)
eststo contactscaleLeft

reg deprivationscale treat   `ics' `mcs' if lrcat==2, vce(cluster id_gemeinde)
eststo deprivationscaleCenter
reg fearscale treat  `ics' `mcs' if lrcat==2, vce(cluster id_gemeinde)
eststo fearscaleCenter
reg alienationscale treat  `ics' `mcs' if lrcat==2, vce(cluster id_gemeinde)
eststo alienationscaleCenter
reg discordscale treat  `ics' `mcs' if lrcat==2, vce(cluster id_gemeinde)
eststo discordscaleCenter
reg empathy treat  `ics' `mcs' if lrcat==2, vce(cluster id_gemeinde)
eststo empathyCenter
reg contactscale treat `ics' `mcs' if lrcat==2, vce(cluster id_gemeinde)
eststo contactscaleCenter

reg deprivationscale treat   `ics' `mcs' if lrcat==3, vce(cluster id_gemeinde)
eststo deprivationscaleRight
reg fearscale treat  `ics' `mcs' if lrcat==3, vce(cluster id_gemeinde)
eststo fearscaleRight
reg alienationscale treat  `ics' `mcs' if lrcat==3, vce(cluster id_gemeinde)
eststo alienationscaleRight
reg discordscale treat  `ics' `mcs' if lrcat==3, vce(cluster id_gemeinde)
eststo discordscaleRight
reg empathy treat  `ics' `mcs' if lrcat==3, vce(cluster id_gemeinde)
eststo empathyRight
reg contactscale treat `ics' `mcs' if lrcat==3, vce(cluster id_gemeinde)
eststo contactscaleRight


coefplot (deprivationscaleLeft, label("Left")) (deprivationscaleCenter, label("Center")) (deprivationscaleRight, label("Right")), bylabel(Deprivation/competition) xline(0) ///
		|| fearscaleLeft fearscaleCenter fearscaleRight, bylabel(Insecurity) /// 
		|| alienationscaleLeft alienationscaleCenter alienationscaleRight, bylabel(Alienation) /// 
		|| discordscaleLeft discordscaleCenter discordscaleRight, bylabel(Community/elite discord) /// 
		|| empathyLeft empathyCenter empathyRight, bylabel(Empathy) ///  
        || contactscaleLeft contactscaleCenter contactscaleRight,  bylabel(Contact) ///  
keep(treat) byopts(cols(3)) ylab("") xscale(range(-.5 (.25)  .5)) xlabel(-.5 (.25)  .5) name(mechanismplotLrcat, replace) ///
aspectratio(0.5) subtitle( ,bcolor(white) bmargin(bottom) lcolor(gs8))  legend(rows(1)) 

graph display mechanismplotLrcat, ysize(1) xsize(2.2) scale(1.3) scheme(plotplain) 
graph export "output/mechanismplotLrcat.pdf", replace


****(b) Authoritarianism****
local ics female age i.partnership parent hhmembers edu 
local mcs km2 pop14 popdens14 unempl_100 frauen_a alter_d ab65_a share_m_15_25 auslaender_a kkr_ewa05_1000 part_rate shareCDU2013 shareSPD2013 shareLINKE2013 shareAFD2013 i.sn_l

reg deprivationscale treat   `ics' `mcs' if authoritarianism==1, vce(cluster id_gemeinde)
eststo deprivationscaleLib
reg fearscale treat  `ics' `mcs' if authoritarianism==1, vce(cluster id_gemeinde)
eststo fearscaleLib
reg alienationscale treat  `ics' `mcs' if authoritarianism==1, vce(cluster id_gemeinde)
eststo alienationscaleLib
reg discordscale treat  `ics' `mcs' if authoritarianism==1, vce(cluster id_gemeinde)
eststo discordscaleLib
reg empathy treat  `ics' `mcs' if authoritarianism==1, vce(cluster id_gemeinde)
eststo empathyLib
reg contactscale treat `ics' `mcs' if authoritarianism==1, vce(cluster id_gemeinde)
eststo contactscaleLib

reg deprivationscale treat   `ics' `mcs' if authoritarianism==2, vce(cluster id_gemeinde)
eststo deprivationscaleMod
reg fearscale treat  `ics' `mcs' if authoritarianism==2, vce(cluster id_gemeinde)
eststo fearscaleMod
reg alienationscale treat  `ics' `mcs' if authoritarianism==2, vce(cluster id_gemeinde)
eststo alienationscaleMod
reg discordscale treat  `ics' `mcs' if authoritarianism==2, vce(cluster id_gemeinde)
eststo discordscaleMod
reg empathy treat  `ics' `mcs' if authoritarianism==2, vce(cluster id_gemeinde)
eststo empathyMod
reg contactscale treat `ics' `mcs' if authoritarianism==2, vce(cluster id_gemeinde)
eststo contactscaleMod

reg deprivationscale treat   `ics' `mcs' if authoritarianism==3, vce(cluster id_gemeinde)
eststo deprivationscaleAuth
reg fearscale treat  `ics' `mcs' if authoritarianism==3, vce(cluster id_gemeinde)
eststo fearscaleAuth
reg alienationscale treat  `ics' `mcs' if authoritarianism==3, vce(cluster id_gemeinde)
eststo alienationscaleAuth
reg discordscale treat  `ics' `mcs' if authoritarianism==3, vce(cluster id_gemeinde)
eststo discordscaleAuth
reg empathy treat  `ics' `mcs' if authoritarianism==3, vce(cluster id_gemeinde)
eststo empathyAuth
reg contactscale treat `ics' `mcs' if authoritarianism==3, vce(cluster id_gemeinde)
eststo contactscaleAuth


coefplot (deprivationscaleLib, label("Liberal")) (deprivationscaleMod, label("Moderate")) (deprivationscaleAuth, label("Authoritarian")), bylabel(Deprivation/competition) xline(0) ///
		|| fearscaleLib fearscaleMod fearscaleAuth, bylabel(Insecurity) /// 
		|| alienationscaleLib alienationscaleMod alienationscaleAuth, bylabel(Alienation) /// 
		|| discordscaleLib discordscaleMod discordscaleAuth, bylabel(Community/elite discord) /// 
		|| empathyLib empathyMod empathyAuth, bylabel(Empathy) ///  
        || contactscaleLib contactscaleMod contactscaleAuth,  bylabel(Contact) ///  
keep(treat) byopts(cols(3)) ylab("") xscale(range(-.5 (.25)  .5)) xlabel(-.5 (.25)  .5) name(mechanismplotAuth, replace) ///
aspectratio(0.5) subtitle( ,bcolor(white) bmargin(bottom) lcolor(gs8))  legend(rows(1)) 

graph display mechanismplotAuth, ysize(1) xsize(2.2) scale(1.3) scheme(plotplain) 
graph export "output/mechanismplotAuth.pdf", replace


////////////C  Allocation of refugees////////

***Table 6A: Allocation of refugees at the county level*********
* Landkreis
use "dta_files/municip14.dta", clear
keep if east==1 
collapse (sum) km2 pop14 n_recipients2015, by(sn_l landkreis)
gen popdens14 = pop14/km2
gen percpop_recip15 = (n_recipients2015/pop14)*100
save sums, replace
use "dta_files/municip14.dta", clear
keep if east==1 
collapse (mean) unemployment frauen_a alter_d ab65_a share_men15_25years auslaender_a kkr_ewa05 part_rate shareCDU shareSPD shareLINKE shareOTHERS leerstandsquote [fw=pop14], by(sn_l landkreis)
merge 1:1 sn_l landkreis using sums
reg percpop_recip15 pop14 popdens14 unemployment frauen_a alter_d ab65_a share_men15_25years auslaender_a kkr_ewa05 part_rate shareCDU shareSPD shareLINKE shareOTHERS leerstandsquote i.sn_l


********Difference in available housing due to diverging economic trends******
use "dta_files/ind.dta", clear

//////////Figure 12A//////////////
// Pre-treatment trends in population numbers, housing and purchasing power
use "dta_files/municip17.dta", clear
collapse (mean) ewa_a_gesamt05 ewa_a_gesamt15 a_haeuser05 a_haeuser15 a_haushalt05 a_haushalt15 a_gewerbe05 a_gewerbe15 a_wohngeb05 a_wohngeb15 kkr_w_summe05 kkr_w_summe15 kkr_ewa05 kkr_ewa15, by(treat)
rename *05 *2005
rename *15 *2015
gen id = _n
reshape long ewa_a_gesamt a_haeuser a_haushalt a_gewerbe a_wohngeb kkr_w_summe kkr_ewa, i(id) j(year) 
label variable ewa_a_gesamt "Nr of inhabitants"
label variable a_haeuser "Nr of houses"
label variable a_haushalt "Nr of households"
label variable a_gewerbe "Nr of businesses"
gen kkr_w_summe1000 = kkr_w_summe/1000000
label variable kkr_w_summe1000 "Purchasing power (mio EUR)"
label variable kkr_ewa "Purchasing power per capita"
label variable year "Year"
label define treat 0 "Control" 1 "Treatment"
label values treat treat

foreach v of varlist ewa_a_gesamt a_haeuser a_haushalt a_gewerbe kkr_ewa kkr_w_summe1000{
	format %8.0fc `v'
	line `v' year, by(treat, note("")) name(`v', replace) subtitle( ,bcolor(white) bmargin(bottom) lcolor(gs8))
}
graph combine ewa_a_gesamt a_haeuser a_haushalt a_gewerbe kkr_w_summe1000 kkr_ewa, col(3) ///
name(municiptrends, replace)
graph display municiptrends, ysize(1) xsize(1.5) scale(1) scheme(plotplain)
*note("Differences-in-differences/differences in trends in no cases statistically significant (all p>0.5).", size(vsmall)) ///
graph export "output/municiptrends.pdf", replace



///////////////////////////////////////
//D Regression tables and additional results//
/////////////////////////////////////

*****************************************
*************Table 7A********************
*****************************************
use "dta_files/ind.dta", clear

local ics female age i.partnership parent hhmembers edu 
local mcs km2 pop14 popdens14 unempl_100 frauen_a alter_d ab65_a share_m_15_25 auslaender_a kkr_ewa05_1000 part_rate shareCDU2013 shareSPD2013 shareLINKE2013 shareAFD2013 i.sn_l 

** Individual level 
* binary treatment 
eststo clear
reg biasscale treat `ics' `mcs', vce(cluster id_gemeinde)
eststo biasscale
reg rwscale treat `ics' `mcs', vce(cluster id_gemeinde)
eststo rwscale
reg popscale treat `ics' `mcs', vce(cluster id_gemeinde)
eststo popscale
reg refscale treat `ics' `mcs', vce(cluster id_gemeinde)
eststo refscale

ksmirnov biasscale, by(treat)
ksmirnov rwscale, by(treat)
ksmirnov popscale, by(treat)
ksmirnov refscale, by(treat)

* percentages
reg biasscale percpop_recip15 `ics' `mcs', vce(cluster id_gemeinde)
eststo biasscalep
reg rwscale percpop_recip15 `ics' `mcs', vce(cluster id_gemeinde)
eststo rwscalep
reg popscale percpop_recip15 `ics' `mcs', vce(cluster id_gemeinde)
eststo popscalep
reg refscale percpop_recip15 `ics' `mcs', vce(cluster id_gemeinde)
eststo refscalep

** Municipality level
preserve

merge m:1 gemeinde_no using "dta_files/municip16.dta", keepusing(shareCDU2016 shareSPD2016 shareLINKE2016 shareAFD2016 shareGREENS2016 shareFDP2016)
drop if _merge==2
drop _merge

foreach v of var * { 
	local l`v' : variable label `v' 
}
collapse (mean) female age partnership parent hhmembers edu  km2 pop14 popdens14 unempl_100 frauen_a alter_d ab65_a share_m_15_25 auslaender_a kkr_ewa05_1000 part_rate shareCDU2013 shareSPD2013 shareLINKE2013 shareAFD2013  npartysimple ruling_party_affil leerstandsquote share_CDU2017 share_SPD2017 share_LINKE2017 share_AFD2017 share_afd2017 n_recipients2014 recip14_per_1000 n_recipients2015 percpop_recip15 recip15_per_1000 sn_l sfemales15 sreception15 scommunal15 sdecentral15 brand demo koerperver unterkunft any_attack shareCDU2016 shareSPD2016 shareLINKE2016 shareAFD2016 shareGREENS2016 shareFDP2016 treat, by(id_gemeinde)
foreach v of var * { 
	label var `v' "`l`v''" 
}

local mcs km2 pop14 popdens14 unempl_100 frauen_a alter_d ab65_a share_m_15_25 auslaender_a kkr_ewa05_1000 part_rate shareCDU2013 shareSPD2013 shareLINKE2013 shareAFD2013 i.sn_l 
gen treat_5=treat

* binary treatment
reg share_AFD2017 treat_5 `mcs'
gen share_afd201710 = share_afd2017*10
reg share_afd201710 treat `mcs'
eststo share_AFD201710

reg share_AFD2017 treat `mcs'
eststo share_AFD2017
ksmirnov share_AFD2017, by(treat)

* Percentages
reg share_AFD2017 percpop_recip15 `mcs'
eststo share_AFD2017p
reg share_AFD2017 percpop_recip15  `mcs' if percpop_recip15<20
eststo share_AFD2017p_no_outlier

***Table 7A***
esttab share_AFD2017 rwscale refscale popscale biasscale , b(2) se(2) lab r2(2) varwidth(30) nogaps compress booktabs wide ///
order(treat female age *partnership parent hhmembers edu )

restore

****Figure 13A*****
// Plot of outcomes on individual-level predictors, put in treatment effect in perspective
use "dta_files/ind.dta", clear

cap drop edum
recode edu 0=1, gen(edum)
label values edum edu
local icsd i.female i.agecats i.partnership i.parent i.hhmembers i.edum 
local mcs km2 pop14 popdens14 unempl_100 frauen_a alter_d ab65_a share_m_15_25 auslaender_a kkr_ewa05_1000 part_rate shareCDU2013 shareSPD2013 shareLINKE2013 shareAFD2013 i.sn_l [pw=agecatweight] 

** Individual level 
* binary treatment 
eststo clear
reg biasscale treat `icsd' `mcs', vce(cluster id_gemeinde)
eststo biasscaled
reg rwscale treat `icsd' `mcs', vce(cluster id_gemeinde)
eststo rwscaled
reg popscale treat `icsd' `mcs', vce(cluster id_gemeinde)
eststo popscaled
reg refscale treat `icsd' `mcs', vce(cluster id_gemeinde)
eststo refscaled

coefplot    rwscaled, xline(0) bylabel(Right-wing support)  ///
		 || refscaled, xline(0) bylabel(Anti-refugee sentiments)  ///
		 || popscaled,  xline(0) bylabel(Populism) ///
		 || biasscaled, xline(0) bylabel(Behavioral bias) ///
keep(treat *female *agecats *partnership *parent *hhmembers *edum )  /// 
order(deprivationscale fearscale alienationscale discordscale empathy contactscale) ///
 xlabel(, format(%4.2f)) subtitle( ,bcolor(white) bmargin(bottom) lcolor(gs8)) ///
 name(persp, replace) byopts(ixaxes) baselevel label
 graph display persp, ysize(1) xsize(1.25) scale(0.75) scheme(plotplain) 
graph export "output/persp.pdf", replace


//////////Table 8A//////////
* Regression of outcomes on theoretical mechanisms
local ics female age i.partnership parent hhmembers edu 
local mcs km2 pop14 popdens14 unempl_100 frauen_a alter_d ab65_a share_m_15_25 auslaender_a kkr_ewa05_1000 part_rate shareCDU2013 shareSPD2013 shareLINKE2013 shareAFD2013 i.sn_l

reg biasscale deprivationscale fearscale alienationscale discordscale empathy contactscale  `ics' `mcs', vce(cluster id_gemeinde) 
eststo biasscalem
reg rwscale deprivationscale fearscale alienationscale discordscale empathy contactscale  `ics' `mcs', vce(cluster id_gemeinde) 
eststo rwscalem
reg popscale deprivationscale fearscale alienationscale discordscale empathy contactscale  `ics' `mcs', vce(cluster id_gemeinde) 
eststo popscalem
reg refscale deprivationscale fearscale alienationscale discordscale empathy contactscale  `ics' `mcs', vce(cluster id_gemeinde) 
eststo refscalem
estat vif

* Table 8A
esttab  rwscalem refscalem popscalem biasscalem,  b(2) se(2) lab r2(2) varwidth(35) nogaps wide compress booktabs /// 
 keep( deprivationscale fearscale alienationscale discordscale empathy contactscale _cons) indicate("Individual controls = *female" "Municipality controls = *sn_l") 


////////Table 9A///////
** Regression of mechanisms on treatment
local ics female age i.partnership parent hhmembers edu 
local mcs km2 pop14 popdens14 unempl_100 frauen_a alter_d ab65_a share_m_15_25 auslaender_a kkr_ewa05_1000 part_rate shareCDU2013 shareSPD2013 shareLINKE2013 shareAFD2013 i.sn_l

reg deprivationscale treat  `ics' `mcs', vce(cluster id_gemeinde) 
eststo deprivationscale
reg fearscale treat  `ics' `mcs', vce(cluster id_gemeinde) 
eststo fearscale
reg alienationscale treat  `ics' `mcs', vce(cluster id_gemeinde) 
eststo alienationscale
reg discordscale treat  `ics' `mcs', vce(cluster id_gemeinde) 
eststo discordscale
reg empathy treat  `ics' `mcs', vce(cluster id_gemeinde) 
eststo empathy
reg contactscale treat `ics' `mcs', vce(cluster id_gemeinde) 
eststo contactscale

* Table 9A
esttab deprivationscale fearscale alienationscale discordscale empathy contactscale, b(2) se(2) lab r2(2) varwidth(35) nogaps compress wide booktabs

 
/////Figure 14A & Figure 15A////////
** Regressions of individual outcome measures on treatment indicator
use "dta_files/ind.dta", clear
cap drop treat_*
local ics female age i.partnership parent hhmembers edu 
local mcs km2 pop14 popdens14 unempl_100 frauen_a alter_d ab65_a share_m_15_25 auslaender_a kkr_ewa05_1000 part_rate shareCDU2013 shareSPD2013 shareLINKE2013 shareAFD2013 i.sn_l

gen treat_1=treat
gen treat_2=treat
gen treat_3=treat
gen treat_4=treat
gen treat_5=treat
gen treat_6=treat
gen treat_7=treat
gen treat_8=treat

* Behavioral bias
reg dg_inbias_nstd treat_1 `ics' `mcs', vce(cluster id_gemeinde)
eststo outcome1
reg tg_inbias_nstd treat_2 `ics' `mcs', vce(cluster id_gemeinde)
eststo outcome2

* Right-wing support 
reg ethnocent_assert treat_1 `ics' `mcs', vce(cluster id_gemeinde)
eststo outcome3
reg ethnocent_proud treat_2 `ics' `mcs' , vce(cluster id_gemeinde)
eststo outcome4
reg xenophob_exploit treat_3 `ics' `mcs' , vce(cluster id_gemeinde)
eststo outcome5
reg xenophob_welfare treat_4 `ics' `mcs' , vce(cluster id_gemeinde)
eststo outcome6
reg foreigner_work treat_5 `ics' `mcs' , vce(cluster id_gemeinde)
eststo outcome7
reg foreigner_pension treat_6 `ics' `mcs', vce(cluster id_gemeinde) 
eststo outcome8
reg rwvote treat_7 `ics' `mcs', vce(cluster id_gemeinde) 
eststo outcome9
reg afdthermometer treat_8 `ics' `mcs', vce(cluster id_gemeinde) 
eststo outcome10

* Populism
reg pop_people treat_1 `ics' `mcs' , vce(cluster id_gemeinde)
eststo outcome11
reg pop_compro treat_2 `ics' `mcs', vce(cluster id_gemeinde) 
eststo outcome12
reg pop_satis treat_3 `ics' `mcs', vce(cluster id_gemeinde) 
eststo outcome13

* Reject refugees
reg refrestr_war treat_1 `ics' `mcs', vce(cluster id_gemeinde) 
eststo outcome14
reg refrestr_persec treat_2 `ics' `mcs', vce(cluster id_gemeinde) 
eststo outcome15
reg refrestr_econ treat_3 `ics' `mcs', vce(cluster id_gemeinde) 
eststo outcome16
reg refrestr_fam treat_4 `ics' `mcs', vce(cluster id_gemeinde)
eststo outcome17
reg refcompete treat_5 `ics' `mcs', vce(cluster id_gemeinde) 
eststo outcome18
reg refdeport treat_6 `ics' `mcs', vce(cluster id_gemeinde) 
eststo outcome19
reg refprotect treat_7 `ics' `mcs', vce(cluster id_gemeinde)
eststo outcome20

////////Figure 14A/////////////
coefplot ///
		   outcome1, bylabel(DG bias) ///
		|| outcome2, bylabel(TG bias) /// 
		|| outcome3, bylabel(National assert) /// 
		|| outcome4, bylabel(Proud German) ///  
		|| outcome5, bylabel(Foreign welfare exploit) ///  
		|| outcome6, bylabel(Child support Germans) /// 
		|| outcome7, bylabel(Foreign unwanted work) ///  
		|| outcome8, bylabel(Foreign pensions) ///  
		|| outcome9, bylabel(Right-wing vote) ///  
		|| outcome10, bylabel(Support AfD) ///
keep(treat_*) byopts(cols(5)) xline(0) name(outcomes1, replace) ///
ylab("", notick) yscale(lstyle(none)) xlabel(, format(%4.2f)) subtitle( ,bcolor(white) bmargin(bottom) lcolor(gs8)) 
graph export "output/outcomes1.pdf", replace

coefplot ///
		   outcome11, bylabel(People decide) ///
		|| outcome12, bylabel(Compromise in democracy) /// 
		|| outcome13, bylabel(Satisfaction democracy) /// 
		|| outcome14, bylabel(Restrict war refugees) ///  
		|| outcome15, bylabel(Restrict persecuted) ///  
		|| outcome16, bylabel(Restrict economic) /// 
		|| outcome17, bylabel(Restrict refugee family) ///  
		|| outcome18, bylabel(Fear econ competition) ///  
		|| outcome19, bylabel(Deport rejected) ///  
		|| outcome20, bylabel(No need to protect) ///
keep(treat_*) byopts(cols(5)) xline(0) name(outcomes2, replace) ///
ylab("") yscale(lstyle(none)) xlabel(, format(%4.2f)) subtitle( ,bcolor(white) bmargin(bottom) lcolor(gs8)) 
graph export "output/outcomes2.pdf", replace


//////////Figure 15A///////////
* Deprivation
reg depr_lowinc treat_1 `ics' `mcs', vce(cluster id_gemeinde) 
eststo mechanism1
reg depr_unempl treat_2 `ics' `mcs', vce(cluster id_gemeinde)  
eststo mechanism2
reg depr_finan treat_3 `ics' `mcs', vce(cluster id_gemeinde)  
eststo mechanism3
reg depr_unfair treat_4 `ics' `mcs', vce(cluster id_gemeinde)  
eststo mechanism4
reg depr_second treat_5 `ics' `mcs', vce(cluster id_gemeinde) 
 eststo mechanism5
reg depr_losing treat_6 `ics' `mcs', vce(cluster id_gemeinde)  
eststo mechanism6

* Insecurity
reg fear_alone treat_1 `ics' `mcs' , vce(cluster id_gemeinde) 
eststo mechanism7
reg fear_theft treat_2 `ics' `mcs', vce(cluster id_gemeinde)  
eststo mechanism8
reg fear_violence treat_3 `ics' `mcs', vce(cluster id_gemeinde) 
eststo mechanism9

* Alienation
reg alien_enrich treat_1 `ics' `mcs', vce(cluster id_gemeinde)  
eststo mechanism10
reg alien_stranger treat_2 `ics' `mcs', vce(cluster id_gemeinde)  
eststo mechanism11
reg alien_islam treat_3 `ics' `mcs', vce(cluster id_gemeinde)  
eststo mechanism12
reg alien_muslim treat_4 `ics' `mcs' , vce(cluster id_gemeinde) 
eststo mechanism13

* Discord
reg disc_polit treat_1 `ics' `mcs', vce(cluster id_gemeinde)  
eststo mechanism14
reg disc_comm treat_2 `ics' `mcs', vce(cluster id_gemeinde)  
eststo mechanism15
reg disc_discuss treat_3 `ics' `mcs', vce(cluster id_gemeinde)  
eststo mechanism16
reg disc_dem treat_4 `ics' `mcs', vce(cluster id_gemeinde)  
eststo mechanism17
reg disc_help treat_5 `ics' `mcs', vce(cluster id_gemeinde)  
eststo mechanism18

* Empathy
reg empathy_nstd treat_1 `ics' `mcs' , vce(cluster id_gemeinde) 
eststo mechanism19

* Contact
reg contactrefugees treat_1 `ics' `mcs', vce(cluster id_gemeinde) 
eststo mechanism20
reg supportrefugees2 treat_2 `ics' `mcs', vce(cluster id_gemeinde) 
eststo mechanism21
reg aquaintances treat_3 `ics' `mcs', vce(cluster id_gemeinde) 
eststo mechanism22

* Figure 15A
coefplot ///
		   mechanism1, bylabel(Lowest quartile) ///
		|| mechanism2, bylabel(Unemployed) /// 
		|| mechanism3, bylabel(Bad econ situation) /// 
		|| mechanism4, bylabel(Less than fair) ///  
		|| mechanism5, bylabel(Second class) ///  
		|| mechanism6, bylabel(Losing side) /// 
		|| mechanism7, bylabel(Fear alone) ///  
		|| mechanism8, bylabel(Fear theft) ///  
		|| mechanism9, bylabel(Fear violence) ///  
		|| mechanism10, bylabel(Culturally enrich) ///  
		|| mechanism11, bylabel(Stranger own land) ///  
		|| mechanism12, bylabel(Islam extent reach) ///
keep(treat_*) byopts(cols(6)) xline(0) name(mechanisms1, replace) ///
ylab("", notick) yscale(lstyle(none)) xlabel(, format(%4.2f)) subtitle( ,bcolor(white) bmargin(bottom) lcolor(gs8)) 
graph export "output/mechanisms1.pdf", replace

coefplot ///
		   mechanism13, bylabel(Muslim reject const) ///
		|| mechanism14, bylabel(Authorities favor) /// 
		|| mechanism15, bylabel(Refugee topic divided) /// 
		|| mechanism16, bylabel(Opinions differ) ///  
		|| mechanism17, bylabel(Not understand demo) ///  
		|| mechanism18, bylabel(Not understand help) /// 
		|| mechanism19, bylabel(Feel pity) ///  
		|| mechanism20, bylabel(Contact on street) ///  
		|| mechanism21, bylabel(Provided support) ///  
		|| mechanism22, bylabel(Foreign acquaintances) ///  
keep(treat_*) byopts(cols(6)) xline(0) name(mechanisms2, replace) order(treat_1 treat_2 treat_3 treat_4 treat_5) ///
ylab("", notick) yscale(lstyle(none)) xlabel(, format(%4.2f)) subtitle( ,bcolor(white) bmargin(bottom) lcolor(gs8)) 
graph export "output/mechanisms2.pdf", replace


/////////Heterogenous treatment effects/////////
////////Table 10A///////////
* Left-right
local ics female age i.partnership parent hhmembers edu 
local mcs km2 pop14 popdens14 unempl_100 frauen_a alter_d ab65_a share_m_15_25 auslaender_a kkr_ewa05_1000 part_rate shareCDU2013 shareSPD2013 shareLINKE2013 shareAFD2013 i.sn_l 

reg rwscale treat##lrcat `ics' `mcs', vce(cluster id_gemeinde) 
eststo rwscalelr
reg refscale treat##lrcat `ics' `mcs', vce(cluster id_gemeinde) 
eststo refscalelr
reg popscale treat##lrcat `ics' `mcs', vce(cluster id_gemeinde) 
eststo popscalelr
reg biasscale treat##lrcat `ics' `mcs', vce(cluster id_gemeinde)  
eststo biasscalelr
esttab rwscalelr refscalelr popscalelr biasscalelr, ///
keep(1.treat#1.lrcat 1.treat#2.lrcat 1.treat#3.lrcat 1.treat 1.lrcat 2.lrcat 3.lrcat _cons) order(1.treat#1.lrcat 1.treat#2.lrcat 1.treat#3.lrcat 1.lrcat 2.lrcat 3.lrcat  1.treat female sn_l _cons) ///
b(2) se(2) lab r2(2) varwidth(35) nogaps compress wide booktabs ///
indicate("Individual controls = *female" "Municipality controls = *sn_l")

///////Table 11A////////
* Authoritarianism 
reg rwscale treat##authoritarianism `ics' `mcs', vce(cluster id_gemeinde) 
eststo rwscaleauth
reg refscale treat##authoritarianism `ics' `mcs', vce(cluster id_gemeinde) 
eststo refscaleauth
reg popscale treat##authoritarianism `ics' `mcs', vce(cluster id_gemeinde) 
eststo popscaleauth
reg biasscale treat##authoritarianism `ics' `mcs', vce(cluster id_gemeinde) 
eststo biasscaleauth

esttab rwscaleauth refscaleauth popscaleauth biasscaleauth, ///
keep(1.treat#1.authoritarianism 1.treat#2.authoritarianism 1.treat#3.authoritarianism 1.treat 1.authoritarianism 2.authoritarianism 3.authoritarianism _cons) order(1.treat#1.authoritarianism 1.treat#2.authoritarianism 1.treat#3.authoritarianism 1.authoritarianism 2.authoritarianism 3.authoritarianism  1.treat female sn_l _cons) ///
b(2) se(2) lab r2(2) varwidth(35) nogaps compress wide booktabs ///
indicate("Individual controls = *female" "Municipality controls = *sn_l")


/////////////Figure 16A//////////////
use "dta_files/ind.dta", clear
local ics female age i.partnership parent hhmembers edu 
local mcs km2 pop14 popdens14 unempl_100 frauen_a alter_d ab65_a share_m_15_25 auslaender_a kkr_ewa05_1000 part_rate shareCDU2013 shareSPD2013 shareLINKE2013 shareAFD2013 i.sn_l [pw=agecatweight] 

** Individual level 
* binary treatment 
eststo clear
reg biasscale treat `ics' `mcs', vce(cluster id_gemeinde)
eststo biasscale
reg rwscale treat `ics' `mcs', vce(cluster id_gemeinde)
eststo rwscale
reg popscale treat `ics' `mcs', vce(cluster id_gemeinde)
eststo popscale
reg refscale treat `ics' `mcs', vce(cluster id_gemeinde)
eststo refscale

ksmirnov biasscale, by(treat)
ksmirnov rwscale, by(treat)
ksmirnov popscale, by(treat)
ksmirnov refscale, by(treat)

* percentages
reg biasscale percpop_recip15 `ics' `mcs', vce(cluster id_gemeinde)
eststo biasscalep
reg rwscale percpop_recip15 `ics' `mcs', vce(cluster id_gemeinde)
eststo rwscalep
reg popscale percpop_recip15 `ics' `mcs', vce(cluster id_gemeinde)
eststo popscalep
reg refscale percpop_recip15 `ics' `mcs', vce(cluster id_gemeinde)
eststo refscalep

** Municipality level
preserve
merge m:1 gemeinde_no using "dta_files/municip16.dta", keepusing(shareCDU2016 shareSPD2016 shareLINKE2016 shareAFD2016 shareGREENS2016 shareFDP2016)
drop if _merge==2
drop _merge

foreach v of var * { 
	local l`v' : variable label `v' 
}
collapse (mean) female age partnership parent hhmembers edu  km2 pop14 popdens14 unempl_100 frauen_a alter_d ab65_a share_m_15_25 auslaender_a kkr_ewa05_1000 part_rate shareCDU2013 shareSPD2013 shareLINKE2013 shareAFD2013  npartysimple ruling_party_affil leerstandsquote share_CDU2017 share_SPD2017 share_LINKE2017 share_AFD2017 share_afd2017 n_recipients2014 recip14_per_1000 n_recipients2015 percpop_recip15 recip15_per_1000 sn_l sfemales15 sreception15 scommunal15 sdecentral15 brand demo koerperver unterkunft any_attack shareCDU2016 shareSPD2016 shareLINKE2016 shareAFD2016 shareGREENS2016 shareFDP2016 treat, by(id_gemeinde)
foreach v of var * { 
	label var `v' "`l`v''" 
}

local mcs km2 pop14 popdens14 unempl_100 frauen_a alter_d ab65_a share_m_15_25 auslaender_a kkr_ewa05_1000 part_rate shareCDU2013 shareSPD2013 shareLINKE2013 shareAFD2013 i.sn_l 
gen treat_5=treat

* binary treatment
reg share_AFD2017 treat_5 `mcs'
gen share_afd201710 = share_afd2017*10
reg share_afd201710 treat `mcs'
eststo share_AFD201710
reg share_AFD2017 treat `mcs'
eststo share_AFD2017
ksmirnov share_AFD2017, by(treat)

* Percentages
reg share_AFD2017 percpop_recip15 `mcs'
eststo share_AFD2017p
reg share_AFD2017 percpop_recip15  `mcs' if percpop_recip15<20
eststo share_AFD2017p_no_outlier

* Figure 16A
coefplot share_AFD201710, bylabel(AfD 2017) xline(0) ///
		|| rwscale, bylabel(Right-wing support) /// 
		|| refscale, bylabel(Anti-refugee sentiments) /// 
		|| popscale, bylabel(Populism) /// 
		|| biasscale, bylabel(Behavioral bias) ///  
keep(treat) byopts(cols(3) hole(4)) xscale(range(-.1 (.05)  .1)) xlabel(-.1 (.05) .1, format(%4.2f))  name(mainregress, replace) ///
ylab("") subtitle( ,bcolor(white) bmargin(bottom) lcolor(gs8))  aspectratio(0.5) 
graph display mainregress, ysize(1) xsize(1.9) scale(1.25) scheme(plotplain) 
graph export "output/ageweightregress.pdf", replace

restore

/////////Robustness checks////////

/////Table 12A///////
use "dta_files/ind.dta", clear
local ics female age i.partnership parent hhmembers edu 
local mcs km2 pop14 popdens14 unempl_100 frauen_a alter_d ab65_a share_m_15_25 auslaender_a kkr_ewa05_1000 part_rate shareCDU2013 shareSPD2013 shareLINKE2013 shareAFD2013 i.sn_l 

** Individual level 
* binary treatment 
eststo clear
reg biasscale treat `ics' `mcs', vce(cluster id_gemeinde)
eststo biasscale
reg rwscale treat `ics' `mcs', vce(cluster id_gemeinde)
eststo rwscale
reg popscale treat `ics' `mcs', vce(cluster id_gemeinde)
eststo popscale
reg refscale treat `ics' `mcs', vce(cluster id_gemeinde)
eststo refscale

ksmirnov biasscale, by(treat)
ksmirnov rwscale, by(treat)
ksmirnov popscale, by(treat)
ksmirnov refscale, by(treat)

* percentages
reg biasscale percpop_recip15 `ics' `mcs', vce(cluster id_gemeinde)
eststo biasscalep
reg rwscale percpop_recip15 `ics' `mcs', vce(cluster id_gemeinde)
eststo rwscalep
reg popscale percpop_recip15 `ics' `mcs', vce(cluster id_gemeinde)
eststo popscalep
reg refscale percpop_recip15 `ics' `mcs', vce(cluster id_gemeinde)
eststo refscalep

** Municipality level
preserve
merge m:1 gemeinde_no using "dta_files/municip16.dta", keepusing(shareCDU2016 shareSPD2016 shareLINKE2016 shareAFD2016 shareGREENS2016 shareFDP2016)
drop if _merge==2
drop _merge

foreach v of var * { 
	local l`v' : variable label `v' 
}
collapse (mean) female age partnership parent hhmembers edu  km2 pop14 popdens14 unempl_100 frauen_a alter_d ab65_a share_m_15_25 auslaender_a kkr_ewa05_1000 part_rate shareCDU2013 shareSPD2013 shareLINKE2013 shareAFD2013  npartysimple ruling_party_affil leerstandsquote share_CDU2017 share_SPD2017 share_LINKE2017 share_AFD2017 share_afd2017 n_recipients2014 recip14_per_1000 n_recipients2015 percpop_recip15 recip15_per_1000 sn_l sfemales15 sreception15 scommunal15 sdecentral15 brand demo koerperver unterkunft any_attack shareCDU2016 shareSPD2016 shareLINKE2016 shareAFD2016 shareGREENS2016 shareFDP2016 treat, by(id_gemeinde)
foreach v of var * { 
	label var `v' "`l`v''" 
}

local mcs km2 pop14 popdens14 unempl_100 frauen_a alter_d ab65_a share_m_15_25 auslaender_a kkr_ewa05_1000 part_rate shareCDU2013 shareSPD2013 shareLINKE2013 shareAFD2013 i.sn_l 
gen treat_5=treat

* binary treatment
reg share_AFD2017 treat_5 `mcs'
gen share_afd201710 = share_afd2017*10
reg share_afd201710 treat `mcs'
eststo share_AFD201710
reg share_AFD2017 treat `mcs'
eststo share_AFD2017
ksmirnov share_AFD2017, by(treat)

* Percentages
reg share_AFD2017 percpop_recip15 `mcs'
eststo share_AFD2017p
reg share_AFD2017 percpop_recip15  `mcs' if percpop_recip15<20
eststo share_AFD2017p_no_outlier

* Table 12A
esttab share_AFD2017p share_AFD2017p_no_outlier rwscalep refscalep popscalep biasscalep,  b(2) se(2) lab r2(2) varwidth(30) nogaps compress booktabs wide ///
order(percpop_recip15 female age *partnership parent hhmembers edu )

restore 

//////Figure 17A////////
* Outlier plot
use "dta_files/ind.dta", clear
local ics female age i.partnership parent hhmembers edu 
local mcs km2 pop14 popdens14 unempl_100 frauen_a alter_d ab65_a share_m_15_25 auslaender_a kkr_ewa05_1000 part_rate shareCDU2013 shareSPD2013 shareLINKE2013 shareAFD2013 i.sn_l [pw=agecatweight] 

** Individual level 
* binary treatment 
eststo clear
reg biasscale treat `ics' `mcs', vce(cluster id_gemeinde)
eststo biasscale
reg rwscale treat `ics' `mcs', vce(cluster id_gemeinde)
eststo rwscale
reg popscale treat `ics' `mcs', vce(cluster id_gemeinde)
eststo popscale
reg refscale treat `ics' `mcs', vce(cluster id_gemeinde)
eststo refscale

ksmirnov biasscale, by(treat)
ksmirnov rwscale, by(treat)
ksmirnov popscale, by(treat)
ksmirnov refscale, by(treat)

* percentages
reg biasscale percpop_recip15 `ics' `mcs', vce(cluster id_gemeinde)
eststo biasscalep
reg rwscale percpop_recip15 `ics' `mcs', vce(cluster id_gemeinde)
eststo rwscalep
reg popscale percpop_recip15 `ics' `mcs', vce(cluster id_gemeinde)
eststo popscalep
reg refscale percpop_recip15 `ics' `mcs', vce(cluster id_gemeinde)
eststo refscalep

** Municipality level
preserve
merge m:1 gemeinde_no using "dta_files/municip16.dta", keepusing(shareCDU2016 shareSPD2016 shareLINKE2016 shareAFD2016 shareGREENS2016 shareFDP2016)
drop if _merge==2
drop _merge

foreach v of var * { 
	local l`v' : variable label `v' 
}
collapse (mean) female age partnership parent hhmembers edu  km2 pop14 popdens14 unempl_100 frauen_a alter_d ab65_a share_m_15_25 auslaender_a kkr_ewa05_1000 part_rate shareCDU2013 shareSPD2013 shareLINKE2013 shareAFD2013  npartysimple ruling_party_affil leerstandsquote share_CDU2017 share_SPD2017 share_LINKE2017 share_AFD2017 share_afd2017 n_recipients2014 recip14_per_1000 n_recipients2015 percpop_recip15 recip15_per_1000 sn_l sfemales15 sreception15 scommunal15 sdecentral15 brand demo koerperver unterkunft any_attack shareCDU2016 shareSPD2016 shareLINKE2016 shareAFD2016 shareGREENS2016 shareFDP2016 treat, by(id_gemeinde)
foreach v of var * { 
	label var `v' "`l`v''" 
}

local mcs km2 pop14 popdens14 unempl_100 frauen_a alter_d ab65_a share_m_15_25 auslaender_a kkr_ewa05_1000 part_rate shareCDU2013 shareSPD2013 shareLINKE2013 shareAFD2013 i.sn_l 
gen treat_5=treat

* binary treatment
reg share_AFD2017 treat_5 `mcs'
gen share_afd201710 = share_afd2017*10
reg share_afd201710 treat `mcs'
eststo share_AFD201710
reg share_AFD2017 treat `mcs'
eststo share_AFD2017
ksmirnov share_AFD2017, by(treat)

* Percentages
reg share_AFD2017 percpop_recip15 `mcs'
eststo share_AFD2017p
reg share_AFD2017 percpop_recip15  `mcs' if percpop_recip15<20
eststo share_AFD2017p_no_outlier

*Figure 17A
twoway (scatter share_AFD2017 recip15_per_1000, name(ex, replace)) (lowess share_AFD2017 recip15_per_1000), legend(off)
twoway (scatter share_AFD2017 recip15_per_1000  if recip15_per_1000<200, name(noex,replace)) (lowess share_AFD2017 recip15_per_1000 if recip15_per_1000<200), legend(off)
graph combine ex noex , col(2) row(1) name(outlier, replace)

restore

////////Figure 18A//////////////////
* Perceived vs real treatment status
use "dta_files/ind.dta", clear
local ics female age i.partnership parent hhmembers edu 
local mcs km2 pop14 popdens14 unempl_100 frauen_a alter_d ab65_a share_m_15_25 auslaender_a kkr_ewa05_1000 part_rate shareCDU2013 shareSPD2013 shareLINKE2013 shareAFD2013 i.sn_l

reg biasscale i.perctreat `ics' `mcs', vce(cluster id_gemeinde) 
eststo biasscaleperc
reg rwscale i.perctreat `ics' `mcs', vce(cluster id_gemeinde) 
eststo rwscaleperc
reg popscale i.perctreat `ics' `mcs', vce(cluster id_gemeinde) 
eststo popscaleperc
reg refscale i.perctreat `ics' `mcs', vce(cluster id_gemeinde) 
eststo refscaleperc

esttab biasscaleperc rwscaleperc popscaleperc refscaleperc, keep(1.perctreat 2.perctreat 3.perctreat 4.perctreat) b(2) se(2) lab r2(2) varwidth(35) nogaps compress wide  star(* .1 ** .05 ** .01)

* Figure 18A
coefplot rwscaleperc, bylabel(Right-wing support) /// 
		|| refscaleperc, bylabel(Anti-refugee sentiments) /// 
		|| popscaleperc, bylabel(Populism) /// 
		|| biasscaleperc, bylabel(Behavioral bias) ///  
keep(*perctreat) byopts(cols(2) ) xscale(range(-.3 (.1)  .3)) xlabel(-.3 (.1) .3, format(%4.2f))  name(hetperception, replace) ///
ylab("", notick) yscale(lstyle(none)) subtitle( ,bcolor(white) bmargin(bottom) lcolor(gs8))  aspectratio(0.5) xline(0)
graph display hetperception, ysize(1) xsize(1.9) scale(1.25) scheme(plotplain) 
graph export "output/hetperception.pdf", replace



///////Table 13A & Table 14A////////
// Robustness check: anti-immigrant attacks
use "dta_files/Bencec_Strasheim.dta", clear
*drop observations after August 2015
drop if date>20331

* counters
bys gemeinde_no: egen demonstration=count(category_e) if category_e=="demonstration"
bys gemeinde_no: egen arson=count(category_e) if category_e=="arson" | category_e=="arson & miscellaneous attack"
bys gemeinde_no: egen assault=count(category_e) if category_e=="assault" 
bys gemeinde_no: egen vandalism=count(category_e) if category_e=="miscellaneous attack" 

* collapse and merge
collapse demonstration arson assault vandalism, by(gemeinde_no)
save "attacks.dta", replace
use "dta_files/municipality_level_variables.dta"
merge 1:1 gemeinde_no using attacks
drop if _merge==2
drop _merge

* replace missing values
replace arson=0 if arson==.
replace demonstration =0 if demonstration ==.
replace assault =0 if assault ==.
replace vandalism =0 if vandalism ==.

* labelling
label variable demonstration "Anti-refugee demonstration"
label variable arson "Arson against refugee housing"
label variable assault "Bodily assault of refugee"
label variable vandalism "Vandalism against refugee housing"


// Moran's I and spatial lag model 
use "dta_files/ind.dta", clear
preserve

foreach v of var * { 
	local l`v' : variable label `v' 
}
collapse (mean) gemeinde_no treat recip15_per_1000 biasscale rwscale popscale refscale share_AFD2017 latitude longitude female age partnership parent hhmembers edu km2 pop14 popdens14 unempl_100 frauen_a alter_d ab65_a share_m_15_25 auslaender_a kkr_ewa05_1000 part_rate shareCDU2013 shareSPD2013 shareLINKE2013 shareAFD2013 p_mena p_ssa p_other p_eur sn_l, by(id_gemeinde)

foreach v of var * { 
	label var `v' "`l`v''" 
}

tab sn_l, gen(sni)
drop if refscale==.
gen treat_1=treat
gen treat_2=treat
gen treat_3=treat
gen treat_4=treat
gen treat_5=treat

spwmatrix gecon longitude latitude, wname(wdist) eignvar(weign) cart rowstand wtype(inv) alpha(1) replace

* Moran's I
spatgsa biasscale, weights(wdist) moran 
spatgsa rwscale, weights(wdist) moran
spatgsa popscale, weights(wdist) moran
spatgsa refscale, weights(wdist) moran
spatgsa share_AFD2017, weights(wdist) moran


** Regression models
local ics female age partnership parent hhmembers edu 
local mcs km2 pop14 popdens14 unempl_100 frauen_a alter_d ab65_a share_m_15_25 auslaender_a kkr_ewa05_1000 part_rate shareCDU2013 shareSPD2013 shareLINKE2013 shareAFD2013 sni2 sni3 sni4 sni5


spmlreg biasscale treat `ics' `mcs', weights(wdist) wfrom(Stata) eignvar(weign) model(lag) 
eststo biasscalesp
spmlreg rwscale treat  `ics' `mcs', weights(wdist) wfrom(Stata) eignvar(weign) model(lag) 
eststo rwscalesp
spmlreg popscale treat  `ics' `mcs', weights(wdist) wfrom(Stata) eignvar(weign) model(lag) 
eststo popscalesp
spmlreg refscale treat  `ics' `mcs', weights(wdist) wfrom(Stata) eignvar(weign) model(lag) 
eststo refscalesp
spmlreg share_AFD2017 treat  `ics' `mcs', weights(wdist) wfrom(Stata) eignvar(weign) model(lag) 
eststo afdsp

***Table 14A*****
esttab afdsp rwscalesp refscalesp popscalesp biasscalesp, b(2) se(2) lab r2(2) varwidth(30) nogaps compress booktabs wide

restore 




/////////Prevented refugee allocation due to violence against refuees or refugee housing///////
/////////Table 15A////////////
*'Prevented' allocation due to violence
use "dta_files/ind.dta", clear
preserve
foreach v of var * { 
	local l`v' : variable label `v' 
}
collapse (mean) km2 pop14 popdens14 unempl_100 frauen_a alter_d ab65_a share_m_15_25 auslaender_a kkr_ewa05_1000 part_rate shareCDU2013 shareSPD2013 shareLINKE2013 shareAFD2013  npartysimple ruling_party_affil leerstandsquote share_CDU2017 share_SPD2017 share_LINKE2017 share_AFD2017 turnout17 n_recipients2014 recip14_per_1000 n_recipients2015 recip15_per_1000 n_recipients2016 percpop_recip16 demonstration arson vandalism assault sn_l sfemales15 sreception15 scommunal15 sdecentral15 treat, by(id_gemeinde)
foreach v of var * { 
	label var `v' "`l`v''" 
} 
bys treat: egen obs = count(id_gemeinde)
label variable obs "N"

eststo overalltreat: estpost tabstat demonstration arson vandalism assault, columns(statistics) stats(mean sd) missing  
eststo bygrouptreat: estpost tabstat demonstration arson vandalism assault obs, by(treat) columns(statistics) stats(mean sd) missing nototal 
esttab overalltreat bygrouptreat,  main(mean %9.2f) aux(sd %9.2f) unstack  nonote  noobs compress label nogaps  title("Attacks against refugees and housing") nonumber varwidth(35) replace wide booktabs

restore 



/////Attrition///////
/////Table 16A/////// 
eststo overallout: estpost tabstat neighborsmovingin neighborsmovingout residence4years , columns(statistics) stats(mean sd) missing  
eststo bygroupout: estpost tabstat neighborsmovingin neighborsmovingout residence4years, by(treat) columns(statistics) stats(mean sd) missing nototal
esttab overallout bygroupout,  main(mean %9.2f) aux(sd %9.2f) unstack  nonote  noobs compress label nogaps  title("Manipulation checks and attrition") nonumber varwidth(45) replace  wide booktabs


//////Origin effects/////////
// Potential influence of place of origin (Table 17A and Figure 19A)
use "dta_files/ind.dta", clear

local ics female age i.partnership parent hhmembers edu 
local mcs km2 pop14 popdens14 unempl_100 frauen_a alter_d ab65_a share_m_15_25 auslaender_a kkr_ewa05_1000 part_rate shareCDU2013 shareSPD2013 shareLINKE2013 shareAFD2013 i.sn_l 

reg rwscale treat p_mena p_ssa p_other p_eur  `ics' `mcs', vce(cluster id_gemeinde) 
eststo rwscaleorig
reg refscale p_mena p_ssa p_eur p_other `ics' `mcs', vce(cluster id_gemeinde) 
eststo refscaleorig
reg popscale p_mena p_ssa p_eur p_other `ics' `mcs', vce(cluster id_gemeinde) 
eststo popscaleorig
reg biasscale p_mena p_ssa p_eur p_other `ics' `mcs', vce(cluster id_gemeinde)  
eststo biasscaleorig


* Plot and AfD share regression
* Deviations plot (Figure 19A)
preserve
	foreach var of varlist p_mena p_ssa p_other p_eur{
	devnplot `var' if treat==1, name(`var', replace) ylabel(,nogrid) 
	}
	graph combine p_mena p_ssa p_eur p_other, col(2) name(deviationplots, replace)
graph display deviationplots, ysize(1) xsize(2) scale(1.3) scheme(plotplain) 
graph export "output/Fig19a.pdf", replace


foreach v of var * { 
	local l`v' : variable label `v' 
}
collapse (mean) gemeinde_no treat recip15_per_1000 biasscale rwscale popscale refscale share_AFD2017 latitude longitude female age partnership parent hhmembers edu km2 pop14 popdens14 unempl_100 frauen_a alter_d ab65_a share_m_15_25 auslaender_a kkr_ewa05_1000 part_rate shareCDU2013 shareSPD2013 shareLINKE2013 shareAFD2013 p_mena p_ssa p_other p_eur sn_l, by(id_gemeinde)
foreach v of var * { 
	label var `v' "`l`v''" 
}

* Regression models
local ics female age partnership parent hhmembers edu 
local mcs km2 pop14 popdens14 unempl_100 frauen_a alter_d ab65_a share_m_15_25 auslaender_a kkr_ewa05_1000 part_rate shareCDU2013 shareSPD2013 shareLINKE2013 shareAFD2013 i.sn_l

reg share_AFD2017 p_mena p_ssa p_other p_eur `ics' `mcs'
eststo afdorig
restore

* Regression of outcomes on place-of-origin shares (Table 17A)
esttab afdorig rwscaleorig refscaleorig popscaleorig biasscaleorig, ///
keep(p_mena p_ssa p_eur p_other _cons) order(p_mena p_ssa p_eur p_other _cons) ///
b(2) se(2) lab r2(2) varwidth(35) nogaps compress wide  ///
indicate("Individual controls = *female" "Municipality controls = *sn_l") booktabs


//////Simulation of selection on unobservables///////
/////Figure 23A//////////

foreach name in share_AFD2017 rwscale refscale popscale biasscale {
use "dta_files/`name'_simulation.dta"
sum yX_maxcor
local cmax = r(mean) // Saves the mean value of the highest correlate in a local macro
twoway ///
		(lowess p_treat cor_wz, yline(0.05)  ///
		ytitle("P-value treat") xtitle("corr({it:y, {&omega}})") title("`name'", size(medium))  ///
		xlab(, format(%03.2f) nogrid) xline(`cmax', lpattern(dot)) ylab(,format(%03.2f) nogrid)) ///
		(scatter p_treat cor_wz, msymbol(i) xaxis(2) yaxis(2) ///
		xtitle("", axis(2)) legend(off) ytitle("", axis(2)) ///
		xlab(`cmax' "{it:max} corr({it:y, X})" , notick axis(2)) ylab(0.05 "*0.05", notick axis(2)) name(simul_`name', replace))
}

graph combine simul_share_AFD2017 simul_rwscale simul_refscale simul_popscale simul_biasscale, col(3) name(biassumalation, replace)



/////Sampling and recruitment procedures///////
/////Table 18A//////////////////
*Excel file "Table 18A"


/////Table 19A/////
use "dta_files/ind.dta", clear
tab agecat female, cell nofreq


/////Figure 24A//////
// Response rates for treatment and control over time 
use "dta_files/ind.dta", clear
sort treat start, stable
gen counter=1
cumul counter, by(treat) freq gen(obsovertime)
twoway (line obsovertime start if treat==0) (line obsovertime start if treat==1)

* Recruitment rate per week (should be the same)
gen day= dofc(start)
format %td day
gen week = wofd(day)
format %tw week
order day week, after(start)
bys treat week: egen obsperweek = count(counter)
replace obsperweek = obsperweek*(808/512) if treat==0
twoway (line obsperweek week if treat==0) (line obsperweek week if treat==1)
